// 游学
layui.use(['jquery', 'element', 'table', 'layer', 'form','layedit', 'upload'], function(){
    var element = layui.element;
    var $ = layui.$;
    var form = layui.form;
    var studyToursTable = layui.table;
    var layedit = layui.layedit;
    var ctxPath = "/education";
    var upload = layui.upload;

    var uploadInst = upload.render({
        elem: '#showImage',
        url: ctxPath + '/file/uploadPic',
        before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#previewImg').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            if(res.code > 0){
                return layer.msg('上传失败');
            }
            //上传成功
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#previewText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini img-reload">重试</a>');
            demoText.find('.img-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

    //http://www.layui.com/doc/modules/layedit.html
    var detailEditId = layedit.build('detail', {
        uploadImage :  {
            url: ctxPath + '/file/uploadPic',
            type: 'post'
        }
    });

    studyToursTable.render({
        id: 'studyToursTableId',
        elem: '#studyToursTableId',
        url: ctxPath + '/studyTours/tableData',
        page: true,
        method: 'post',
        cols:[[
            {field:'id', title:'ID', width: 50},
            {field:'name', title:'名称', width: 200},
            {field:'code', title:'code', width: 100 },
            {field:'generation', title:'年龄段', width: 100 },
            {field:'country', title:'国家', width: 100 },
            {field:'price', title:'价格', width: 100 },
            {field:'detail', title:'详情', width: 300},
            {field:'schedule', title:'行程', width: 300},
            {fixed: 'right', width:200, align:'center', toolbar: '#studyToursBar'}
        ]],
        done: function(res, curr, count){
            //如果是异步请求数据方式，res即为你接口返回的信息。
            //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
            console.log(res);

            //得到当前页码
            console.log(curr);

            //得到数据总量
            console.log(count);
        }
    });

    var active = {
        searchStudyAbroadProduct: function(){
            var name = $('#name').val();
            var generation = $('#generation').val();
            var country = $('#country').val();
            if(!!name) {
                name = '%' + $('#name').val() + '%';
            }
            studyToursTable.reload('studyToursTableId', {
                where: {
                    name: name,
                    generation: generation,
                    country: country
                }
            });
        },
        cancelBtn: function(e) {
            window.location.href = ctxPath + '/studyTours/list';
        }
    };

    //按钮事件
    $('.layui-btn').on('click', function(e){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //监听工具条
    studyToursTable.on('tool(studyTours)', function(obj){
        var data = obj.data ,layEvent = obj.event;
        if(layEvent === 'detail'){
            location.href = ctxPath + '/studyTours/toView?id=' + data.id;
            initFormForUpdateOrView(data.id);
        } else if(layEvent === 'delete'){
            console.log(data.id);
            layer.confirm('确定删除？', function(index){
                //向服务端发送删除指令
                $.ajax({
                    url: ctxPath + '/studyTours/delete',
                    data: {'id' : data.id},
                    type: 'post',
                    dataType: 'json',
                    success: function (result) {
                        if(result.code == '0') {
                            layer.alert('删除成功！', function(index){
                                obj.del(); //删除对应行（tr）的DOM结构
                                layer.close(index);
                            });
                        } else {
                            layer.alert("删除失败：" + result.msg);
                        }
                    }
                });
            });
        } else if(layEvent === 'edit'){
            location.href = ctxPath + '/studyTours/toModify?id=' + data.id;
        }
        return false;
    });

    //表单提交
    form.on('submit(saveBtn)', function(data){
        $.ajax({
            url: ctxPath + '/studyTours/saveOrUpdate',
            data: data.field,
            type: 'post',
            dataType: 'json',
            success: function (result) {
                if(result.code == '0') {
                    layer.alert('保存成功！', function(index){
                        layer.close(index);
                        location.href = ctxPath + '/studyTours/list';
                    });
                } else {
                    layer.alert("保存失败：" + result.msg);
                }
            }
        });
        return false;
    });
});
